<template>
  <div class="vue-box sbot sbot-fluid">
    <!-- 参数栏 -->
    <div class="c-panel">
      <el-form :inline="true">
        <!-- 表单 -->
        <sa-item v-model="m.avatar" type="img" name="头像" br />
        <sa-item v-model="m.nickName" type="text" name="昵称" br />
        <sa-item v-model="m.name" type="text" name="用户名" br />
        <sa-item v-model="m.password" type="password" name="密码" br />
        <sa-item v-model="m.confirmPwd" type="password" name="确认密码" br />
        <sa-item v-model="m.phone" type="num" name="手机" br max-length="11" />

        <sa-item name="角色" br>
          <el-select v-model="m.roleId">
            <el-option label="请选择" :value="0" disabled />
            <el-option v-for="role in roleList" :key="role.id" :label="role.name" :value="role.id" />
          </el-select>
        </sa-item>
        <sa-item v-model="m.status" type="enum" jtype="3" :jv="dims.YES_OR_NO" name="启用状态" brbr />
        <sa-item v-model="m.note" type="text" name="描述" br />
        <sa-item v-model="m.sort" type="num" name="序号" br max-length="11" />
        <sa-item name="" br class="s-ok">
          <el-button type="primary" icon="el-icon-plus" @click="ok()">保存</el-button>
        </sa-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import sa from '@/sa-frame/sa';


export default {
  name: 'acc-add',
  props: { param: Object },
  data() {
    return {
      // 超级对象
      m: this.createModel(),
      dims: this.param.dims,
      roleList: this.param.roleList
    }
  },
  created: function () {
    this.roleList = this.param.roleList
  },
  methods: {
    createModel() {
      return {
        name: null,
        avatar: null,
        password: null,
        phone: null,
        roleId: null,
        status: null,
        note: null,
        sort: null
      }
    },
    // 变更
    ok: function () {
      // 表单校验
      let m = this.m;
      sa.checkNull(m.avatar, '请选择一个头像');
      sa.checkNull(m.name, '请输入昵称');
      sa.checkNull(m.phone, '请输入电话号码');
      sa.checkNull(m.roleId, '请选择角色');
      sa.checkNull(m.password, '请输入密码');
      sa.check(m.confirmPwd !== m.password, '密码不一致');
      sa.checkNull(m.status, '请确认状态');


      // 添加
      sa.ajax('/sso/add', m, function (res) {
        sa.alert('添加成功, 账号id为：' + res.data, function () {
          this.m = this.createModel();
          if (res.data) {
            sa.closeModel();
            sa.currView().f5();
          }
        }.bind(this));
      }.bind(this));
    }
  }
}
</script>

<style scoped></style>
